package com.ib.review.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ib.review.mapper.CompareRecordMapper;
import com.ib.review.mapper.CompareResultMapper;
import com.ib.review.mapper.CompareRuleMapper;
import com.ib.review.pojo.dto.CompareParam;
import com.ib.review.pojo.dto.CompareRecordQuery;
import com.ib.review.pojo.dto.CompareResultQuery;
import com.ib.review.pojo.dto.CompareRoleParam;
import com.ib.review.pojo.po.CompareRecordPO;
import com.ib.review.pojo.po.CompareResultPO;
import com.ib.review.pojo.po.CompareRulePO;
import com.ib.review.repository.CompareRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public class CompareRepositoryImpl implements CompareRepository {

    @Resource
    private CompareRecordMapper compareRecordMapper;
    @Resource
    private CompareRuleMapper compareRuleMapper;
    @Resource
    private CompareResultMapper compareResultMapper;
    public void insertRecord(CompareRecordPO po) {
        compareRecordMapper.insert(po);
    }

    public void insertRule(CompareRulePO po1) {
        compareRuleMapper.insert(po1);
    }

    public Long selectCountByRecordId(String recordId) {
        QueryWrapper<CompareRecordPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", recordId);
        queryWrapper.eq("del_flag", 0);
        return compareRecordMapper.selectCount(queryWrapper);
    }

    public String queryHeading(CompareParam param) {
        return compareRuleMapper.queryRule(param);
    }

    public List<CompareResultPO> selectByRecordId(CompareResultQuery compareResultQuery) {
        QueryWrapper<CompareResultPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", compareResultQuery.getTaskCode());
        return compareResultMapper.selectList(queryWrapper);
    }

    public void updateFileNames(String recordId, String fileName) {
        UpdateWrapper<CompareRecordPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("record_id", recordId);
        updateWrapper.eq("del_flag", 0);
        updateWrapper.set("file_names", fileName);
        compareRecordMapper.update(null, updateWrapper);
    }

    public List<CompareRecordPO> selectByTimeRange(LocalDateTime localDateTime, String fileName, String userName) {
        QueryWrapper<CompareRecordPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time", localDateTime);
        queryWrapper.eq("creator", userName);
        queryWrapper.eq("del_flag", 0);
        // 模糊查询条件
        if (fileName != null && !fileName.isEmpty()) {
            queryWrapper.like("file_names", fileName);
        }
        // 按创建时间降序排列（最新的在前）
        queryWrapper.orderByDesc("create_time");
        return compareRecordMapper.selectList(queryWrapper);
    }

    public CompareRecordPO queryRecordById(CompareRecordQuery compareRecordQuery) {
        QueryWrapper<CompareRecordPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", compareRecordQuery.getRecordId());
        queryWrapper.eq("del_flag", 0);
        return compareRecordMapper.selectOne(queryWrapper);
    }

    public CompareRulePO queryRuleById(String recordId, int ruleType) {
        return compareRuleMapper.queryRuleById(recordId, ruleType);
    }

    public void updateRoleContent(CompareRoleParam param) {
        compareRuleMapper.updateRoleContent(param);
    }

    public void deleteRule(String ruleId) {
        compareRuleMapper.deleteById(ruleId);
    }

    public List<String> queryFileName(String recordId) {
        return compareRuleMapper.queryFileName(recordId);
    }
}
